package com.zw.admin.server.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zw.admin.server.page.table.PageTableRequest;
import com.zw.admin.server.page.table.PageTableHandler;
import com.zw.admin.server.page.table.PageTableResponse;
import com.zw.admin.server.service.SaleOrderSerivce;
import com.zw.admin.server.page.table.PageTableHandler.CountHandler;
import com.zw.admin.server.page.table.PageTableHandler.ListHandler;
import com.zw.admin.server.utils.HttpServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.zw.admin.server.dao.PSaleOrdersDao;
import com.zw.admin.server.dto.ResponseInfo;
import com.zw.admin.server.model.PSaleOrders;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/pSaleOrderss")
public class PSaleOrdersController {

    @Autowired
    private PSaleOrdersDao pSaleOrdersDao;

    @Autowired
    private SaleOrderSerivce saleOrderSerivce; 
    
    @PostMapping
    @ApiOperation(value = "保存")
    public PSaleOrders save(@RequestBody PSaleOrders pSaleOrders) {
        pSaleOrdersDao.insert(pSaleOrders);

        return pSaleOrders;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public PSaleOrders get(@PathVariable Long id) {
        return pSaleOrdersDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public PSaleOrders update(@RequestBody PSaleOrders pSaleOrders) {
        pSaleOrdersDao.update(pSaleOrders);

        return pSaleOrders;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return pSaleOrdersDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<PSaleOrders> list(PageTableRequest request) {
                return pSaleOrdersDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        pSaleOrdersDao.delete(id);
    }
    
    @PostMapping(value = "/addItem", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存")
    public ResponseInfo addItem(HttpServletRequest request) {
    	JSONObject  requestJo= HttpServletUtil.parameterMap2Json(request);
    	return saleOrderSerivce.addItem(requestJo);

    }
    
    @PostMapping(value = "/saveAll", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存")
    public ResponseInfo saveAll(HttpServletRequest request) {
    	JSONObject  requestJo= HttpServletUtil.parameterMap2Json(request);
    	
    	return saleOrderSerivce.saveAll(requestJo);

    }
    
}
